package com.javakc.platform.modules.system.attachment.service;
import java.sql.Timestamp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.javakc.platform.modules.system.attachment.entity.SystemAttachment;
import com.javakc.platform.modules.system.attachment.dao.SystemAttachmentDao;
import com.javakc.platform.components.base.service.BaseService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* @author zhouhonggang
* @version 1.0.0
* @project rapid-development-platform
* @datetime 2022-04-16
* @description: [系统-附加·逻辑]
*/
@Service
public class SystemAttachmentService extends BaseService<SystemAttachmentDao, SystemAttachment>{

    @Value("${server.image.path}") //用于读取properties文件中动态内容
//   准备动态路径
    private String path;


//    收集插入主键  附加对象封装
    @Transactional
//                   固定性左边    非固定项右边   ...无限个数
    public List<SystemAttachment> upload(MultipartFile... images) throws IOException {

//        定义附件对象list集合
        List<SystemAttachment>list =new ArrayList<>();
//        循环迭代
        for (MultipartFile image : images) {

            //        1、向本地存储

//            获取随机名称
            String uuid = UUID.randomUUID().toString();
//            存储路径
            image.transferTo(Path.of(path + uuid));

            //        2、向数据库写入

//            获取文件原名称
            String name = image.getOriginalFilename();

//               new 实体类  插入数据
            SystemAttachment attachment = new SystemAttachment();
//            文件名称
            attachment.setName(uuid);
//            断言判断name 不为null
            assert name !=null;
//              文件类型
            attachment.setType(name.substring(name.lastIndexOf(".") + 1));
//             文件大小
            attachment.setSizes(image.getSize());
//
            attachment.setPath(path);

//         自己   添加方法
            baseMapper.insert(attachment);

//        3、返回写入集合
            list.add(attachment);
        }

        return list;
    }

    /**
     * 根据条件分页查询
     * @param entity 实体对象
     * @return 分页对象
     */
    public Page<SystemAttachment> page(SystemAttachment entity)
    {
        //封装分页参数
        Page<SystemAttachment> page = Page.of(entity.getPageNo(), entity.getPageSize());
        //封装查询参数
        QueryWrapper<SystemAttachment> wrapper = new QueryWrapper<>();
        //假如name有值则执行查询
        //if(StringUtils.hasLength(entity.getName())) {
        //    wrapper.like("name", entity.getName());
        //}
        //执行分页条件查询
        return baseMapper.selectPage(page, wrapper);
    }

}
